<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>博川</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css" />
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css" />
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/user.css" />
    <style>
        /* 为地址管理和历史订单项添加特定样式 */
        .address-item,
        .order-item {
            color: #333;
            display: flex;
            align-items: center;
            padding: 15px 20px;
        }

        .address-item img,
        .order-item img {
            width: 20px;
            height: 20px;
            margin-right: 10px;
        }

        .address-item span:not(.el-icon-arrow-right),
        .order-item span:not(.el-icon-arrow-right) {
            flex: 1;
            font-size: 16px;
            color: #333;
            text-align: left;
        }

        .address-item .el-icon-arrow-right,
        .order-item .el-icon-arrow-right {
            color: #999;
        }
        /* ... 现有样式保持不变 ... */

        /* 添加订单操作按钮样式 */
        .order-actions {
            margin-top: 15px;
            text-align: center;
            padding: 10px 0;
        }

        .btn-delivery {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #409EFF;
            color: white;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-delivery:hover {
            background-color: #337ecc;
        }

        .btn-delivery:active {
            background-color: #2a68b3;
        }
    </style>
</head>
<body>
<div id="user" class="app">
    <div class="divHead">
        <div class="divTitle">
            <i class="el-icon-arrow-left" @click="goBack"></i>个人中心
        </div>
        <div class="divUser">
            <img src="./../images/headPage.png"/>
            <div class="desc">
                <div class="divName">林之迷 <img src="./../images/women.png"/></div>
                <div class="divPhone">{{userPhone}}</div>
            </div>
        </div>
    </div>
    <div class="divContent">
        <div class="divLinks">
            <div class="item" @click="toAddressPage">
                <img src="./../images/locations.png" alt="地址管理"/>
                <span class="item-text">地址管理</span>
                <i class="el-icon-arrow-right"></i>
            </div>
            <div class="divSplit"></div>
            <div class="item" @click="toOrderPage">
                <img src="./../images/orders.png" alt="历史订单"/>
                <span class="item-text">历史订单</span>
                <i class="el-icon-arrow-right"></i>
            </div>

        </div>
        <!-- 在相应位置添加确认收货按钮 -->
        <div class="divOrders" v-if="order[0] && order[0].id">
            <div class="title">最新订单</div>
            <div class="timeStatus">
                <span>{{order[0].orderTime}}</span>
                <span>{{getStatus(order[0].status)}}</span>
            </div>
            <div class="dishList">
                <div v-for="(item,index) in order[0].orderDetails" :key="index" class="item">
                    <span>{{item.name}}</span>
                    <span>x{{item.number}}</span>
                </div>
            </div>
            <div class="result">
                <span>共{{order[0].sumNum}} 件商品,实付</span>
                <span class="price">￥{{order[0].amount}}</span>
            </div>

            <!-- 添加确认收货按钮 -->
            <div class="order-actions" v-if="order[0].status === 2">
                <button class="btn-delivery" @click="confirmDelivery">确认收货</button>
            </div>

            <div class="btn" v-if="order[0].status === 4">
                <div class="btnAgain" @click="addOrderAgain">再来一单</div>
            </div>
        </div>

        <div class="quitLogin" @click="toPageLogin">
            退出登录
        </div>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant组件 -->
<script src="./../js/vant.min.js"></script>
<!--不同屏幕尺寸根字体设置-->
<script src="./../js/base.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/order.js"></script>
<script src="./../api/login.js"></script>
<script>
    new Vue({
        el:"#user",
        data(){
            return {
                form:{
                    phone:'',
                    code:''
                },
                msgFlag:false,
                order:[{
                    id: undefined,
                    orderTime:'',//下单时间
                    status:undefined,//订单状态 1待付款，2正在派送，3已派送，4已完成，5已取消
                    orderDetails:[{
                        name:'',//菜品名称
                        number:undefined,//数量
                    }],//明细
                    amount:undefined,//实收金额
                    sumNum:0,//菜品总数
                }],
            }
        },
        computed:{},
        created(){
            this.userPhone = sessionStorage.getItem("userPhone")
            this.initData()
        },
        mounted(){},
        methods:{
            goBack(){
                history.go(-1)
            },
            toAddressPage(){
                window.requestAnimationFrame(()=>{
                    window.location.href = '/front/page/address.html'
                })
            },
            toOrderPage(){
                window.requestAnimationFrame(()=>{
                    window.location.href = '/front/page/order.html'
                })
            },
            initData(){
                this.getLatestOrder()
            },
            async getLatestOrder(){
                const params = {
                    page:1,
                    pageSize:1
                }
                const res = await orderPagingApi(params)
                if(res.code === 1){
                    this.order = res.data.records || []
                    if(this.order && this.order.length > 0){
                        // 检查是否返回了sumNum字段
                        if(this.order[0].sumNum !== undefined && this.order[0].sumNum !== null) {
                            // 如果有sumNum字段，直接使用
                            this.order[0].sumNum = this.order[0].sumNum;
                        } else if(this.order[0].orderDetails && this.order[0].orderDetails.length > 0) {
                            // 如果有订单详情，计算商品总数
                            let total = 0;
                            this.order[0].orderDetails.forEach(item => {
                                total += parseInt(item.number) || 0;
                            });
                            this.order[0].sumNum = total;
                        } else {
                            // 如果都没有，设置为空字符串
                            this.order[0].sumNum = '';
                        }

                        // 确保orderDetails字段存在
                        this.order[0].orderDetails = this.order[0].orderDetails || [];
                    }
                }else{
                    this.$notify({ type:'warning', message:res.msg});
                }
            },



            getStatus(status){
                let str = ''
                switch(status){
                    case 1:
                        str =  '待付款'
                        break;
                    case 2:
                        str =  '正在派送'
                        break;
                    case 3:
                        str =  '已派送'
                        break;
                    case 4:
                        str =  '已完成'
                        break;
                    case 5:
                        str =  '已取消'
                        break;
                }
                return str
            },
            // 添加确认收货方法
            // 添加确认收货方法
            async confirmDelivery() {
                if (!this.order || !this.order[0] || !this.order[0].id) {
                    this.$notify({ type:'warning', message: '订单信息不完整'});
                    return;
                }

                try {
                    const res = await updateOrderStatusApi({
                        id: this.order[0].id,
                        status: 3  // 3表示已派送
                    });

                    if(res.code === 1){
                        this.$notify({ type:'success', message: '确认收货成功'});
                        // 更新订单状态显示
                        this.order[0].status = 3;
                    } else {
                        this.$notify({ type:'warning', message: res.msg});
                    }
                } catch (error) {
                    this.$notify({ type:'danger', message: '确认收货失败'});
                    console.error('确认收货异常:', error);
                }
            },


            async addOrderAgain(){
                const res = await orderAgainApi({id:this.order[0].id})
                if(res.code === 1){
                    window.requestAnimationFrame(()=>{
                        window.location.href = '/front/index.html'
                    })
                }else{
                    this.$notify({ type:'warning', message:res.msg});
                }
            },
            async toPageLogin(){
                const res = await loginoutApi()
                if(res.code === 1){
                    window.requestAnimationFrame(()=>{
                        window.location.href = '/front/page/login.html'
                    })
                }else{
                    this.$notify({ type:'warning', message:res.msg});
                }
            }
        }
    })

</script>
</body>
</html>